import mainRequest from '@/service'
import { IDataType } from '../../types'
// import { IDataList } from './types'
import { ElNotification } from 'element-plus'

export function getPageList(pageUrl: string, queryInfo: any) {
  return mainRequest.post<IDataType>({
    url: pageUrl,
    data: queryInfo
  })
}

export function delPageData(url: string) {
  return mainRequest.delete<IDataType>({
    url: url
  })
}

export function createPageData(url: string, data: any) {
  return mainRequest.post<IDataType>({
    url: url,
    data: data
  })
}

export function editPageData(url: string, data: any) {
  return mainRequest.post<IDataType>({
    url: url,
    data: data
  })
}

export function handleReturnData(context: any, pageName: string, res: any) {
  if (res) {
    if (res.code === 200 || res.code === 0) {
      // 3.更新列表数据
      context.dispatch('getPageListAction', {
        pageName: pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
      ElNotification({
        title: '成功',
        message: res.data,
        type: 'success'
      })
    } else {
      if (res.data) {
        ElNotification({
          title: '警告',
          message: res.data,
          type: 'warning'
        })
      } else {
        ElNotification({
          title: '警告',
          message: '抱歉，您不能更改本次信息',
          type: 'warning'
        })
      }
    }
  }
}
